package com.taotao.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import com.taotao.pojo.TbItem;

public class TestBatchSql {
	public static void main(String[] args) throws Exception {
		String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
		Class.forName("com.mysql.jdbc.Driver");
		// 设置url
		String url = "jdbc:mysql://127.0.0.1:3306/day08";// person是数据库名，连接是数据库须要开启
		// 设置用户名
		String username = "root";
		// 设置密码
		String password = "root";
		// 获得连接对象
		Connection connection = DriverManager.getConnection(url, username, password);
		connection.setAutoCommit(false);

		PreparedStatement ps = connection.prepareStatement(sql);
		final int batchSize = 1000;
		int count = 0;
		List<TbItem> items = new ArrayList<>();
		for (TbItem item : items) {
			// ps.setString(1, employee.getName());
			// ps.setString(2, employee.getCity());
			// ps.setString(3, employee.getPhone());
			ps.addBatch();
			if (++count % batchSize == 0) {
				ps.executeBatch();
				connection.commit();
			}
		}
		ps.executeBatch(); // insert remaining records
		connection.commit();
		ps.close();
		connection.close();
	}
}
